#include "jx_uart_send.h"
#include <speech_module_thread.h>

// 串口通信消息头
const unsigned char g_uart_send_head[] = {
    0xaa, 0x55};

// 串口通信消息尾
const unsigned char g_uart_send_foot[] = {
    0x55, 0xaa};

// 十六位整数转32位整数
void _int16_to_int32(uart_param_t *param)
{
  if (sizeof(int) >= 4)
    return;
  unsigned long value = param->d_long;
  unsigned long sign = (value >> 15) & 1;
  unsigned long v = value;
  if (sign)
    v = 0xFFFF0000 | value;
  uart_param_t p;
  p.d_long = v;
  param->d_ucs[0] = p.d_ucs[0];
  param->d_ucs[1] = p.d_ucs[1];
  param->d_ucs[2] = p.d_ucs[2];
  param->d_ucs[3] = p.d_ucs[3];
}

// 浮点数转双精度
void _float_to_double(uart_param_t *param)
{
  if (sizeof(int) >= 4)
    return;
  unsigned long value = param->d_long;
  unsigned long sign = value >> 31;
  unsigned long M = value & 0x007FFFFF;
  unsigned long e = ((value >> 23) & 0xFF) - 127 + 1023;
  uart_param_t p0, p1;
  p1.d_long = ((sign & 1) << 31) | ((e & 0x7FF) << 20) | (M >> 3);
  p0.d_long = (M & 0x7) << 29;
  param->d_ucs[0] = p0.d_ucs[0];
  param->d_ucs[1] = p0.d_ucs[1];
  param->d_ucs[2] = p0.d_ucs[2];
  param->d_ucs[3] = p0.d_ucs[3];
  param->d_ucs[4] = p1.d_ucs[0];
  param->d_ucs[5] = p1.d_ucs[1];
  param->d_ucs[6] = p1.d_ucs[2];
  param->d_ucs[7] = p1.d_ucs[3];
}

// action: PoAlreadyStart
void _uart_PoAlreadyStart()
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PoAlreadyStart;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 3] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 5);
}

// action: PoAlreadyStop
void _uart_PoAlreadyStop()
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PoAlreadyStop;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 3] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 5);
}

// action: PoFinishTimes
void _uart_PoFinishTimes(int po_finish_times)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PoFinishTimes;
  param.d_int = po_finish_times;
  _int16_to_int32(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 7] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 9);
}

// action: PCStateIsOpen
void _uart_PCStateIsOpen()
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PCStateIsOpen;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 3] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 5);
}

// action: PCStateIsShut
void _uart_PCStateIsShut()
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PCStateIsShut;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 3] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 5);
}

// action: PlayNowHoursMin
void _uart_PlayNowHoursMin(char hours, char minutes)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayNowHoursMin;
  param.d_char = hours;
  buff[3] = param.d_uchar;
  param.d_char = minutes;
  buff[4] = param.d_uchar;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 5] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 7);
}

// action: PlayNowYearsMou
void _uart_PlayNowYearsMou(int years, char m, char d, char week)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayNowYearsMou;
  param.d_int = years;
  _int16_to_int32(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  param.d_char = m;
  buff[7] = param.d_uchar;
  param.d_char = d;
  buff[8] = param.d_uchar;
  param.d_char = week;
  buff[9] = param.d_uchar;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 10] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 12);
}

// action: PlayTemp
void _uart_PlayTemp(char temp)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayTemp;
  param.d_char = temp;
  buff[3] = param.d_uchar;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 4] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 6);
}

// action: PlayHumi
void _uart_PlayHumi(char humi)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayHumi;
  param.d_char = humi;
  buff[3] = param.d_uchar;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 4] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 6);
}

// action: PlayTempHumi
void _uart_PlayTempHumi(char temp, char humi)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayTempHumi;
  param.d_char = temp;
  buff[3] = param.d_uchar;
  param.d_char = humi;
  buff[4] = param.d_uchar;
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 5] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 7);
}

// action: PlayBootCount
void _uart_PlayBootCount(int boot_count)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayBootCount;
  param.d_int = boot_count;
  _int16_to_int32(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 7] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 9);
}

// action: PlayPomoCount
void _uart_PlayPomoCount(int pomo_count)
{
  uart_param_t param;
  int i = 0;
  unsigned char buff[UART_SEND_MAX] = {0};
  for (i = 0; i < UART_MSG_HEAD_LEN; i++)
  {
    buff[i + 0] = g_uart_send_head[i];
  }
  buff[2] = U_MSG_PlayPomoCount;
  param.d_int = pomo_count;
  _int16_to_int32(&param);
  buff[3] = param.d_ucs[0];
  buff[4] = param.d_ucs[1];
  buff[5] = param.d_ucs[2];
  buff[6] = param.d_ucs[3];
  for (i = 0; i < UART_MSG_FOOT_LEN; i++)
  {
    buff[i + 7] = g_uart_send_foot[i];
  }
  _uart_send_impl(buff, 9);
}
